﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Starfire3d.Data.States.UnitStates
{
    public class TrackingOrdinance : BaseState<Ordinance>
    {
        private Unit target = null;
        private bool doOnce = false;
        private bool missed = false;
        private bool intercepted = false;

        public override void Execute(Ordinance entity)
        {
            if (target == null)
            {
                target = DataStore.Get<Unit>(entity.Target);
            }

            if (target == null || target.IsDead)
            {
                entity.StateMachine.ChangeState(null);
                return;
            }

            // Things here are only checked once per state update
            if (!doOnce)
            {
                if (entity.Data.GetBool("MISSED"))
                    missed = true;
                else
                {
                    int tgtPd = target.Data.GetInt("PD%");
                    if (tgtPd != -1)
                    {
                        int intChance = tgtPd - entity.Data.GetInt("PENAID");

                        if (RandomGenerator.Rand(100) <= intChance)
                            intercepted = true;
                    }
                }
                doOnce = true;
            }

            if (missed)
            {
                if (entity.DistanceTo(target) <= entity.Data.GetInt("TRAVEL") * 2)
                {
                    entity.StateMachine.ChangeState(null);
                    return;
                }
            }
            
            if (intercepted)
            {
                if (entity.DistanceTo(target) <= 2)
                {
                    entity.StateMachine.ChangeState(null);
                    return;
                }
            }

            if (target.Data.GetInt("X") == entity.Data.GetInt("X") && target.Data.GetInt("Y") == entity.Data.GetInt("Y"))
            {
                target.Sheet.Details.ApplyDamage(entity.Data.GetInt("DMG"), entity.WeaponType);
                //target.Data.Add("HP", target.Data.GetInt("HP") - 10, true);
                entity.StateMachine.ChangeState(null);
                return;
            }

            entity.Data.Add("PX", entity.Data.GetInt("X"), true);
            entity.Data.Add("PY", entity.Data.GetInt("Y"), true);

            if (target.Data.GetInt("X") > entity.Data.GetInt("X"))
                entity.Data.Add("X", entity.Data.GetInt("X") + 1, true);
            if (target.Data.GetInt("X") < entity.Data.GetInt("X"))
                entity.Data.Add("X", entity.Data.GetInt("X") - 1, true);
            if (target.Data.GetInt("Y") > entity.Data.GetInt("Y"))
                entity.Data.Add("Y", entity.Data.GetInt("Y") + 1, true);
            if (target.Data.GetInt("Y") < entity.Data.GetInt("Y"))
                entity.Data.Add("Y", entity.Data.GetInt("Y") - 1, true);
        }

        public override void Exit(Ordinance entity)
        {
            entity.Impacted = true;

            System.Console.SetCursorPosition(entity.Data.GetInt("X"), entity.Data.GetInt("Y"));
            System.Console.Write(".");
        }

        public override string ToString()
        {
            return "Tracking";
        }
    }
}
